package com.lottery.ruok.lottrey.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.retrofitModule.RetrofitBase;
import com.lottery.ruok.lottrey.retrofitModule.RxHelper;
import com.lottery.ruok.lottrey.retrofitModule.RxSubscribe;
import com.lottery.ruok.lottrey.retrofitModule.request.SetWithdrawPasswordRequest;
import com.lottery.ruok.lottrey.userInfo.UserInfo;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/*
* 资金密码
* */

public class MoneyPasswordActivity extends BaseActivity implements TextWatcher{

    @BindView(R.id.name)
    EditText name;
    @BindView(R.id.password)
    EditText password;
    @BindView(R.id.confirm)
    TextView confirm;

    private int source = 0;//0是来自会员中心，1是来自设置界面

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_money_password);
        ButterKnife.bind(this);

        init();
    }

    private void init() {
        source = getIntent().getIntExtra("source", 0);

        name.addTextChangedListener(this);
        password.addTextChangedListener(this);
    }

    @OnClick(R.id.confirm)
    public void onViewClicked() {
        //sendAnalyticMsg("lottery_" + ChargeActivity.ANALYTIC_VERSION + "_me_withdraw_name_pwd_modify");

        //sendAnalyticMsg("lottery_" + SetActivity.ANALYTIC_VERSION + "_me_setting_modifymoneypwd_comfirm");
        RetrofitBase.getRetrofit().create(SetWithdrawPasswordRequest.class).getResult(UserInfo.getToken(),
                name.getText().toString(),
                password.getText().toString(),
                password.getText().toString(),
                null)
                .compose(RxHelper.handleResult())
                .subscribe(new RxSubscribe<Object>(this) {
            @Override
            protected void _onNext(Object modifyProfileResponse) {
                if(source == 0) {
                    Intent in = new Intent(MoneyPasswordActivity.this, BindBankActivity.class);
                    in.putExtra("name", name.getText().toString());
                    startActivity(in);
                }

                finish();
            }

            @Override
            protected boolean showDialog() {
                return true;
            }
        });
    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void afterTextChanged(Editable editable) {
        /*  20180203 改由後端返回錯誤訊息
        if(name.getText().length() > 0 && password.getText().length() == 6){
            confirm.setEnabled(true);
//            confirm.setBackgroundResource(R.drawable.bg_corners_yellow);
        } else {
            confirm.setEnabled(false);
//            confirm.setBackgroundResource(R.drawable.bg_corners_grey);
        }
        */
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }
}
